<template>
  <div>
    <el-page-header @back="$router.back()" :content="$route.name"/>
    <el-main style="margin-top: 20px">
      <el-form>
        <el-form-item>
          <el-form-item label="包裹编号">
            <el-input style="width: calc(100% - 180px); margin-right: 20px"
                      v-model.number="form.id"
                      clearable
                      @keyup.enter.native="getItem"/>
            <el-button
                @click="getItem"
                type="primary"
                icon="el-icon-search">查询
            </el-button>
          </el-form-item>
        </el-form-item>
      </el-form>
    </el-main>
    <el-card>
      <div v-if="item">
        <el-steps :active="currState()" align-center
                  finish-status="success"
                  process-status="finish">
          <el-step v-for="(state, i) in pkgStateList" :key="i"
                   :title="state.label">
            <template slot="description">
              <span v-html="description(i)"></span>
            </template>
          </el-step>
        </el-steps>
      </div>
      <div v-else>
        暂无该包裹信息
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "PkgSearch",
  methods: {
    message(msg, bool) {
      this.$message({
        showClose: true,
        message: msg + (bool ? '成功!' : '失败!'),
        type: (bool ? 'success' : 'error')
      })
    },
    getItem() {
      if (this.form.id === '') {
        this.$message.warning('编号不能为空!')
      } else if (!Number.isInteger(this.form.id)) {
        this.$message.warning('包裹编号必须为数字值!')
      } else {
        const value = Number(this.form.id)
        if (value < 0 || value > 2147483647) {
          this.$message.warning('编号范围有误!')
        } else {
          this.$axios.get(this.$serverUrl
              + '/pkg/get?id=' + this.form.id)
              .then(response => {
                if (response.data) {
                  this.getInfo(response.data)
                } else {
                  this.$message({
                    showClose: true,
                    message: "暂无该包裹信息!",
                    type: "warning"
                  })
                  this.item = ''
                }
              })
              .catch(error => {
                console.log(error)
                this.message('查询', false);
              })
        }
      }
    },
    getInfo(item) {
      this.$axios.get(this.$serverUrl + '/psi/list1?pkg=' + item.id)
          .then(response => {
            this.message('查询', response.data);
            this.info = response.data
            this.item = item
          })
          .catch(error => {
            console.log(error)
            this.message('查询', false);
          })
    },
    currState() {
      const list = this.pkgStateList
      for (let i = 0; i < list.length; i++) {
        if (list[i].label === this.item.currState) {
          return i < 4 ? i : 5;
        }
      }
    },
    description(i) {
      if (i === 0) {
        return this.item.sender + '<br>' + this.item.time
      } else {
        const info = this.info[i - 1]
        return info ?
            info.courier + '<br>' + info.date : ''
      }
    }
  },
  data() {
    return {
      pkgStateList: [],
      form: {id: ''},
      item: {
        id: '',
        desc: '',
        currState: '',
        receiver: '',
        receiverAddr: '',
        sender: '',
        senderAddr: '',
        time: '',
        weight: '',
      },
      info: []
    };
  },
  created() {
    const list = this.$store.state.pkgStateList
    this.pkgStateList = list.slice(1, list.length)
    const item = JSON.parse(sessionStorage.getItem('pkgSearchItem'))
    if (item) {
      sessionStorage.removeItem('pkgSearchItem')
      this.getInfo(item)
      this.form.id = item.id
    } else {
      this.item = ''
    }
    // console.log(this.item)
  },
}
</script>

<style scoped>
</style>